//
//  QYRequestServer.m
//  QingYunZhi
//
//  Created by zhengminjie on 16/7/6.
//  Copyright © 2016年 zhengminjie. All rights reserved.
//

#import "MJRequestServer.h"
#import "Public.h"
#import "AFNetworking.h"

@implementation MJRequestServer

+ (NSString *)getUserid{
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *userid;
    if (IsNilString([ud objectForKey:@"user_id"])) {
        userid = @"0";
    }else{
        userid = [ud objectForKey:@"user_id"];
    }
    return userid;
}

/*
 *通用POST
 */
+ (void)Post:(NSDictionary*)parameters andApiName:(NSString *)apiname andAPI:(NSString *)API andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    NSString *requestPath1=[NSString stringWithFormat:@"%@",API];
    
    
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager POST:requestPath1 parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //[MJProgressHUD showAlertWithString:[NSString stringWithFormat:@"%@",dic]];
        NSLog(@"%@ -------%@",apiname,dic);
        success(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MJProgressHUD showAlertWithString:@"出问题啦！"];
         //[MJProgressHUD showAlertWithString:[NSString stringWithFormat:@"%@",error.userInfo]];
        NSLog(@"errer------%@",error);
    }];
    
}
/*
 *通用get
 */
+ (void)Get:(NSDictionary*)parameters andApiName:(NSString *)apiname andAPI:(NSString *)API andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    NSString *requestPath1=[NSString stringWithFormat:@"%@",API];
    
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager GET:requestPath1 parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        //NSLog(@"totle --- %lld,current --- %lld",downloadProgress.totalUnitCount,downloadProgress.completedUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [responseObject mj_JSONObject];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //[MJProgressHUD showAlertWithString:[NSString stringWithFormat:@"%@",dic]];
        NSLog(@"%@ -------%@",apiname,dic);

        success(dic);
            
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       // [MJProgressHUD showAlertWithString:[NSString stringWithFormat:@"%@",error.userInfo]];
        [MJProgressHUD showAlertWithString:@"出问题啦！"];
        NSLog(@"errer------%@",error);
    }];
    
}

/*
 *优酷视频获取
 */
+ (void)youkuVideoByParameters:(NSDictionary*)parameters andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    
    
    NSString *root = @"http://openapi.youku.com/v2/searches/video/by_keyword.json";
    //NSString *requestPath1=[NSString stringWithFormat:@"%@%@",RootURL,VIDEO_LIST_API];
    
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager GET:root parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSArray *data = dic[@"videos"];
        NSLog(@"所有视频 ------- %@",dic);
        success(data);
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)requestArticleWithHttpArg:(NSString*)HttpArg andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    NSString *str = @"http://apis.baidu.com/showapi_open_bus/channel_news/search_news";
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", str, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"ff9e04c54f25358a161e7413366c8992" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"有问题: %@%ld", error.localizedDescription, error.code);
                                   failure(error);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"新闻---- %@",responseString);
                                   success(responseString);
                               }
                           }];
}






@end
